// main.js (主线程)
const worker = new Worker("fibonacci-worker.js");
document
  .getElementById("calculateButton")
  .addEventListener("click", function () {
    // 获取用户输入的数字
    const num = parseInt(document.getElementById("numberInput").value, 10);

    // 检查是否已经有一个 worker 实例存在
    if (!window.worker) {
      window.worker = new Worker("fibonacci-worker.js");
    }

    // 发送消息给 Worker
    window.worker.postMessage(num);

    // 监听来自 Worker 的消息
    window.worker.onmessage = function (e) {
      document.getElementById(
        "result"
      ).textContent = `Fibonacci(${num}) = ${e.data}`;
    };

    // 处理错误
    window.worker.onerror = function (error) {
      console.error("Worker error: ", error);
    };
  });

